package com.kefu.call.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.call.model.CallGateway;
import com.kefu.call.service.CallGatewayService;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.mybatis.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.call.model.CdrSetmeal;
import com.kefu.call.service.CdrSetmealService;
import com.kefu.call.vo.CdrSetmealSearchVo;
import com.kefu.call.vo.CdrSetmealInsertVo;
import com.kefu.call.vo.CdrSetmealUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "计费_套餐表")
@RestController
@RequestMapping("call/cdr_setmeal")
public class CdrSetmealController extends AbstractBaseController {

    @Autowired
    private CdrSetmealService cdrSetmealService;
    @Autowired
    CallGatewayService callGatewayService;

    @PostMapping("/add")
    @ApiOperation(value = "新增计费_套餐表", notes = "新增计费_套餐表")
    public Result<String> add(@Validated @RequestBody CdrSetmealInsertVo cdrSetmealVo) {
        logger.info("新增计费_套餐表:{}", cdrSetmealVo);

        CdrSetmeal info = cdrSetmealVo.convert();
        cdrSetmealService.save(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除计费_套餐表", notes = "删除计费_套餐表")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除计费_套餐表:{}", delete);
        if (callGatewayService.count(Wrappers.lambdaQuery(CallGateway.class).and(sub -> {
            sub.eq(CallGateway::getSetmealoid, Integer.parseInt(delete.getId())).or().eq(CallGateway::getSetmealoidU, Integer.parseInt(delete.getId()));
        })) > 0) {
            throw new BusinessException("有线路关联了该套餐");
        }
        cdrSetmealService.removeById(Integer.parseInt(delete.getId()));
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改计费_套餐表", notes = "修改计费_套餐表")
    public Result<String> update(@Validated @RequestBody CdrSetmealUpdateVo cdrSetmealVo) {
        logger.info("修改计费_套餐表:{}", cdrSetmealVo);

        CdrSetmeal info = cdrSetmealVo.convert();
        cdrSetmealService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "计费_套餐表详情", notes = "计费_套餐表详情")
    public Result<CdrSetmeal> detail(@Validated @RequestBody DetailVo detail) {

        CdrSetmeal cdrSetmeal = cdrSetmealService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(cdrSetmeal);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询计费_套餐表", notes = "查询计费_套餐表")
    public Result<Page<CdrSetmeal>> list(@Validated @RequestBody CdrSetmealSearchVo search) {

        search.setOrderField("oid");
        search.setOrder(Order.DESC);
        Page<CdrSetmeal> page = cdrSetmealService.searchLambdaPage(search);
        return ResultGenerator.genSuccessResult(page);
    }
}
